package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.dao.mapper.BamuProjectMapper;
import com.yunyao.dao.model.BamuProject;
import com.yunyao.dao.model.BamuSysDept;
import com.yunyao.dao.service.bamu.IBamuProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuProjectServiceImpl extends ServiceImpl<BamuProjectMapper, BamuProject> implements IBamuProjectService {
    @Autowired
    private BamuProjectMapper bamuProjectMapper;

    @Override
    public IPage<BamuProject> pageList(String projectName, String projectType,
                                       List<String> projectList,
                                       String ancestors, Long pageIndex, Long pageSize) {
        // 查询满足条件的总页数
        long pageCount = bamuProjectMapper.pageCount(projectName, projectType, projectList,
                ancestors);
        // 如果总页数为0，则直接返回空的分页对象
        if (pageCount == 0) {
            return new Page<>(pageIndex, pageSize, 0);
        }
        // 计算当前页码对应的数据起始索引
        long index = pageIndex <= 1 ? 0 : (pageIndex - 1) * pageSize;
        // 根据起始索引和每页显示数量获取分页材料列表
        List<BamuProject> pageList = bamuProjectMapper.pageList(projectName, projectType,
                projectList, ancestors, index, pageSize);
        // 创建并设置分页对象的属性
        IPage<BamuProject> page = new Page<>(pageIndex, pageSize, pageCount);
        page.setRecords(pageList);
        // 计算总页数并设置
        page.setPages(pageCount % pageSize == 0 ? pageCount / pageSize : pageCount / pageSize + 1);
        return page;
    }

    @Override
    public BamuProject getByIdNoDel(String id) {
        LambdaQueryWrapper<BamuProject> wrapper = new LambdaQueryWrapper<BamuProject>()
                .eq(BamuProject::getId, id)
                .eq(BamuProject::getIsDeleted, DataDeletedStatus.NO.getType());
        return getOne(wrapper);
    }

    @Override
    public Boolean deleteById(String id) {
        LambdaUpdateWrapper<BamuProject> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(BamuProject::getId, id);
        wrapper.set(BamuProject::getIsDeleted, DataDeletedStatus.YES.getType());
        return update(wrapper);
    }

    @Override
    public BamuProject getByCode(String projectCode, String projectName) {
        LambdaQueryWrapper<BamuProject> wrapper = new LambdaQueryWrapper<BamuProject>()
                .eq(BamuProject::getProjectCode, projectCode)
                .eq(BamuProject::getProjectName, projectName)
                .eq(BamuProject::getIsDeleted, DataDeletedStatus.NO.getType());
        return getOne(wrapper);
    }
}
